//
//  ByViewAimTable.m
//  mijifen_ios
//
//  Created by mc on 2017/10/25.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewAimTable.h"

// cell
#import "ByCellRedPriceOrCount.h"
#import "ByCellRedConditionOrValid.h"
#import "ByCellRedGrant.h"
#import "ByCellAimMemberPhone.h"
#import "ByCellMemberList.h"

// model
#import "ByModelAimMember.h"

@interface ByViewAimTable()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView           *tableView;

@property (nonatomic, copy)   NSArray               *titleArray;
@property (nonatomic, copy)   NSArray               *subArray;
@property (nonatomic, copy)   NSArray               *midArray;
@property (nonatomic, strong) NSMutableDictionary   *infoDic;
@property (nonatomic, assign) BOOL                  isSpecify;     // 是否是指定会员
@property (nonatomic, assign) BOOL                  isNoSpecify;   // 是否是30天内非活跃会员
@property (nonatomic, strong) NSMutableArray        *items;        // 会员列表（测试数据）
@end

@implementation ByViewAimTable
- (void)setupUI{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
    
    [self fetchData];
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.tableView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

#pragma mark - getter
- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"红包金额：",@"使用条件：",@"有效期：",@"发放对象："];
    }
    return _titleArray;
}

- (NSArray *)subArray{
    if (!_subArray) {
        _subArray = @[@"¥",@"以上可用",@"天可用"];
    }
    return _subArray;
}
- (NSArray *)midArray{
    if (!_midArray) {
        _midArray = @[@"支付",@"领取后"];
    }
    return _midArray;
}

- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}

- (NSMutableArray *)items{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

#pragma mark - 获取数据
- (void)fetchData{
    for (int i = 0; i < 10; i ++) {
        ByModelAimMember *model = [[ByModelAimMember alloc] init];
        model.phone = @"12657567567567";
        model.price = @"2308";
        model.cosume = @"23423";
        [self.items addObject:model];
    }
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:4];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArray.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    // 红包金额
    if (indexPath.section == 0) {
        ByCellRedPriceOrCount *cell = [ByCellRedPriceOrCount cellWithTableView:tableView andTitle:self.titleArray[indexPath.section] andSubTitle:self.subArray[indexPath.section]];
        cell.priceOrCountBlock = ^(NSString *priceOrCount) {
            [weakSelf.infoDic setValue:priceOrCount forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
            weakSelf.editBlock([weakSelf.infoDic copy]);
        };
        return cell;
    }
    // 使用条件／有效期
    else if (indexPath.section < 3){
        ByCellRedConditionOrValid *cell = [ByCellRedConditionOrValid cellWithTableView:tableView andTitle:self.titleArray[indexPath.section] andSecTitle:self.midArray[indexPath.section - 1] andThrTitle:self.subArray[indexPath.section]];
        cell.conditionBlock = ^(NSString *condition) {
            [weakSelf.infoDic setValue:condition forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
            weakSelf.editBlock([weakSelf.infoDic copy]);
        };
        return cell;
    }
    // 发放对象
    else if(indexPath.section == 3){
        ByCellRedGrant *cell = [ByCellRedGrant cellWithTableView:tableView andTitle:self.titleArray[indexPath.section]];
        cell.grantBlock = ^(NSString *type) {
            if ([type isEqualToString:@"指定会员"]) {
                weakSelf.isSpecify = YES;
                weakSelf.isNoSpecify = NO;
            }else if([type isEqualToString:@"30天内非活跃会员"]){
                weakSelf.isSpecify = NO;
                weakSelf.isNoSpecify = YES;
                [weakSelf showMemberList];
            }
            NSIndexSet *set = [NSIndexSet indexSetWithIndex:4];
            [weakSelf.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
        };
        return cell;
    }
    // table
    else{
        if (!self.isSpecify) {
            ByCellMemberList *cell = [ByCellMemberList cellWithTableView:tableView];
            cell.hidden = !self.isNoSpecify;
            cell.items = self.items;
            cell.memberListBlock = ^(NSMutableArray *memberAry) {
                NSLog(@"%@",memberAry);
            };
            return cell;
        }else{
            ByCellAimMemberPhone *cell = [ByCellAimMemberPhone cellWithTableView:tableView];
            cell.hidden = !self.isSpecify;
            cell.phoneBlock = ^(NSString *phone) {
                NSLog(@"%@",phone);
            };
            return cell;

        }
    
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isSpecify) {
        return 50;
    }else if (indexPath.section == 4 && !self.isSpecify && self.isNoSpecify){
        return (self.items.count + 2) * 50 + 10;
    }
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *headerView = [[UIView alloc] init];
        UILabel *headerLabel = [[UILabel alloc] init];
        headerLabel.numberOfLines = 0;
        [headerView addSubview:headerLabel];
        NSString *headerStr = @"针对特定会员，刺激回头客，客户永不丢失！";
        CGFloat height = [headerStr getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) font:16].size.height;
        [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView.mas_top).offset(10);
            make.left.equalTo(headerView.mas_left);
            make.size.mas_offset(CGSizeMake(SCREEN_WIDTH, height));
        }];
        headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height + 20);
        headerLabel.text = headerStr;
        headerLabel.font = FONT(16);
        headerLabel.textAlignment = NSTextAlignmentCenter;
        headerLabel.textColor = [UIColor lightGrayColor];
        return headerView;
    }
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return [@"针对特定会员，刺激回头客，客户永不丢失！" getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) font:16].size.height + 20;
    }
    return 0.0001;
}

#pragma mark -
- (void)showMemberList{
    
}

@end
